﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Xml;
using System.IO;

namespace RTPtest1
{

    public partial class nodepopup : Form
    {

        private int _node_id;
        private int _group_id;
        public static Socket socket;
        System.Text.UTF8Encoding encoding = new UTF8Encoding();
        MemoryStream response_stream;
        XmlTextReader xmlreader;

        public int node_id
        {
            get { return _node_id; }
            set { _node_id = value; }
        }

        public int group_id
        {
            get { return _group_id; }
            set { _group_id = value; }
        }

        public nodepopup()
        {
            InitializeComponent();
        }

        private void nodepopup_Load(object sender, EventArgs e)
        {
            this.Text = "Node " + node_id + " Actions & Configuration";
        }

        private void button_actuate_Click(object sender, EventArgs e)
        {
            if (this.Device.SelectedIndex >= 0 && this.Device.SelectedIndex <= 6 && this.State.SelectedIndex >= 0 && this.State.SelectedIndex <= 2)
            {
                try
                {
                    if (this.Device.SelectedIndex > 3 && this.State.SelectedIndex == 2)
                        throw new Exception("Cannot toggle sounder/relays.  Choose on or off.");
                    else if (this.Device.SelectedIndex == 3)
                    {
                        Actuate(node_id, group_id, 0, this.State.SelectedIndex);
                        Actuate(node_id, group_id, 1, this.State.SelectedIndex);
                        Actuate(node_id, group_id, 2, this.State.SelectedIndex);
                    }
                    else
                        Actuate(node_id, group_id, this.Device.SelectedIndex, this.State.SelectedIndex);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message +"\n");
                }
                finally
                {
                }
            }
        }

        private byte[] SendCommand(string command)
        {
            try
            {
                byte[] xmlbytes = encoding.GetBytes(command);
                int size = xmlbytes.Length;
                byte[] bsize = new byte[4];
                bsize = BitConverter.GetBytes(size);

                //need to connect to port 9003 for sending XMLRPC

                byte[] response = new byte[4];
                int response_size;
                socket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
                socket.SendTimeout = 5000;
                socket.ReceiveTimeout = 5000;

                socket.Connect(IPAddress.Parse("127.0.0.1"), 9003);
                if (socket.Connected)
                {
                    socket.Send(bsize, 4, SocketFlags.None);
                    socket.Send(xmlbytes, size, SocketFlags.None);
                    //receiving a response packet to keep stream clear
                    //packet response for actuation is always success, as long as it is received by Xserve
                    //therefore, just reading the packet and ignoring
                    socket.Receive(response, 4, SocketFlags.None);
                    response_size = BitConverter.ToInt32(response, 0);
                    response = new byte[response_size];
                    socket.Receive(response, response_size, SocketFlags.None);

                    //close socket
                    socket.Close();

                    return response;
                }
                else throw new Exception("Could not connect to send XMLRPC.\n");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot perform action: " + ex.Message);
                return null;
            }
            finally
            {
                if (socket != null)
                    socket.Close();
            }
        }
        private void Actuate(int node, int group, int device, int state)
        {
            String command = null;
            //creating the XML command
            command = String.Concat(command, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
            command = String.Concat(command, "<methodName>xmesh.actuate</methodName>");
            command = String.Concat(command, "<params><param><value><struct>");
            command = String.Concat(command, "<member><name>actDevice</name><value><int>" + device + "</int></value></member>");
            command = String.Concat(command, "<member><name>actState</name><value><int>" + state + "</int></value></member>");
            command = String.Concat(command, "<member><name>destAddress</name><value><int>" + node + "</int></value></member>");
            command = String.Concat(command, "<member><name>groupId</name><value><int>" + group + "</int></value></member>");
            command = String.Concat(command, "</struct></value></param></params></methodCall>");
            //sending command, receiving response
            try
            {
                byte[] response = SendCommand(command);
                if (response == null)
                    throw new MotePacketException("Error: No response received\n");
                //need to translate result and retrieve node info
                response_stream = new MemoryStream(response);
                xmlreader = new XmlTextReader(response_stream);
                node_config.Clear();
                string temp;
                while (!xmlreader.EOF)
                {
                    if(xmlreader.ReadToFollowing("name"))
                    {
                        if (xmlreader.Read())
                        {
                            temp = xmlreader.ReadContentAsString();
                            switch (temp)
                            {
                                case "codeName":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            response_log.AppendText(xmlreader.ReadContentAsString() + ": ");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                case "codeDescription":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            response_log.AppendText(xmlreader.ReadContentAsString() + "\n");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                    
                            }
                        }
                            else throw new MotePacketException("Dirty response packet.\n");
                    }   
                }
             }
            catch (Exception ex)
            {
                response_log.AppendText(ex.Message);
            }
            finally
            {
                if (xmlreader != null)
                    xmlreader.Close();
                if (response_stream != null)
                    response_stream.Close();
            }
         }

        private void button_getconfigClicked(object sender, EventArgs e)
        {
            String command = null;
            //creating the XML command
            command = String.Concat(command, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
            command = String.Concat(command, "<methodName>xmesh.get_config</methodName>");
            command = String.Concat(command, "<params><param><value><struct>");
            command = String.Concat(command, "<member><name>destAddress</name><value><int>" + this.node_id + "</int></value></member>");
            command = String.Concat(command, "<member><name>groupId</name><value><int>" + this.group_id + "</int></value></member>");
            command = String.Concat(command, "</struct></value></param></params></methodCall>");
            //sending command, receiving response
            try
            {
                byte[] response = SendCommand(command);
                if (response == null)
                    throw new MotePacketException("Error: No response received\n");
                //need to translate result and retrieve node info
                response_stream = new MemoryStream(response);
                xmlreader = new XmlTextReader(response_stream);
                node_config.Clear();
                string temp;
                while (!xmlreader.EOF)
                {
                    if(xmlreader.ReadToFollowing("name"))
                    {
                        if (xmlreader.Read())
                        {
                            temp = xmlreader.ReadContentAsString();
                            switch (temp)
                            {
                                case "codeName":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            response_log.AppendText(xmlreader.ReadContentAsString() + ": ");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                case "codeDescription":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            response_log.AppendText(xmlreader.ReadContentAsString() + "\n");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                case "nodeId":

                                    if (xmlreader.ReadToFollowing("int"))
                                    {
                                        if (xmlreader.Read())
                                            node_config.AppendText("Node ID: " + xmlreader.ReadContentAsInt() + "\n");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                case "groupId":

                                    if (xmlreader.ReadToFollowing("int"))
                                    {
                                        if (xmlreader.Read())
                                            node_config.AppendText("Group ID: " + xmlreader.ReadContentAsInt() + "\n");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                case "rfPower":
                                    if (xmlreader.ReadToFollowing("int"))
                                    {
                                        if (xmlreader.Read())
                                            node_config.AppendText("RF Power: " + xmlreader.ReadContentAsInt() + "\n");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                case "rfChannel":
                                    if (xmlreader.ReadToFollowing("int"))
                                    {
                                        if (xmlreader.Read())
                                            node_config.AppendText("RF Channel: " + xmlreader.ReadContentAsInt() + "\n");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                case "uidString":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            node_config.AppendText("UID: " + xmlreader.ReadContentAsString() + "\n");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;                          
                            }
                        }
                        else throw new MotePacketException("Dirty response packet.\n");

                    }
                }   
            }
            catch (Exception ex)
            {
                node_config.Clear();
                response_log.AppendText(ex.Message);
            }
            finally
            {
                if (xmlreader != null)
                    xmlreader.Close();
                if (response_stream != null)
                    response_stream.Close();
            }

        }

        private void button_wake_Click(object sender, EventArgs e)
        {
            String command = null;
            //creating the XML command
            command = String.Concat(command, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
            command = String.Concat(command, "<methodName>xmesh.wake</methodName>");
            command = String.Concat(command, "<params><param><value><struct>");
            command = String.Concat(command, "<member><name>destAddress</name><value><int>" + this.node_id + "</int></value></member>");
            command = String.Concat(command, "<member><name>groupId</name><value><int>" + this.group_id + "</int></value></member>");
            command = String.Concat(command, "</struct></value></param></params></methodCall>");
            //sending command, receiving response
            try
            {
                byte[] response = SendCommand(command);
                if (response == null)
                    throw new MotePacketException("Error: No response received\n");
                //need to translate result and retrieve node info
                response_stream = new MemoryStream(response);
                xmlreader = new XmlTextReader(response_stream);
                node_config.Clear();
                string temp;
                while (!xmlreader.EOF)
                {
                    if(xmlreader.ReadToFollowing("name"))
                    {
                        if (xmlreader.Read())
                        {
                            temp = xmlreader.ReadContentAsString();
                            switch (temp)
                            {
                                case "codeName":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            response_log.AppendText(xmlreader.ReadContentAsString() + ": ");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                case "codeDescription":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            response_log.AppendText(xmlreader.ReadContentAsString() + "\n");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                    
                            }
                        }
                            else throw new MotePacketException("Dirty response packet.\n");                      
                     }   
                }
            }
            
            catch (Exception ex)
            {
                response_log.AppendText(ex.Message);
            }
            finally
            {
                if (xmlreader != null)
                    xmlreader.Close();
                if (response_stream != null)
                    response_stream.Close();
            }      
        }

        private void button_sleep_Click(object sender, EventArgs e)
        {
            String command = null;
            //creating the XML command
            command = String.Concat(command, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
            command = String.Concat(command, "<methodName>xmesh.sleep</methodName>");
            command = String.Concat(command, "<params><param><value><struct>");
            command = String.Concat(command, "<member><name>destAddress</name><value><int>" + this.node_id + "</int></value></member>");
            command = String.Concat(command, "<member><name>groupId</name><value><int>" + this.group_id + "</int></value></member>");
            command = String.Concat(command, "</struct></value></param></params></methodCall>");
            //sending command, receiving response
            try
            {
                byte[] response = SendCommand(command);
                if (response == null)
                    throw new MotePacketException("Error: No response received\n");
                //need to translate result and retrieve node info
                response_stream = new MemoryStream(response);
                xmlreader = new XmlTextReader(response_stream);
                node_config.Clear();
                string temp;
                                while (!xmlreader.EOF)
                {
                    if(xmlreader.ReadToFollowing("name"))
                    {
                        if (xmlreader.Read())
                        {
                            temp = xmlreader.ReadContentAsString();
                            switch (temp)
                            {
                                case "codeName":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            response_log.AppendText(xmlreader.ReadContentAsString() + ": ");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                case "codeDescription":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            response_log.AppendText(xmlreader.ReadContentAsString() + "\n");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                    
                            }
                        }
                            else throw new MotePacketException("Dirty response packet.\n");                      
                     }   
                }
            }
            catch (Exception ex)
            {
                response_log.AppendText(ex.Message);
            }
            finally
            {
                if (xmlreader != null)
                    xmlreader.Close();
                if (response_stream != null)
                    response_stream.Close();
            }
        }

        private void Device_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void State_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void xserve_Shutdown()
        {
        }

        private void button_setnodeid_Click(object sender, EventArgs e)
        {
            if (Int32.Parse(choose_nodeid.Text) > 0 && Int32.Parse(choose_nodeid.Text) < 65535)
            {
                if (Int32.Parse(choose_nodeid.Text) == 126 || Int32.Parse(choose_nodeid.Text) == 255)
                {
                    MessageBox.Show("Invalid node ID.\nRange: 1-65534, excluding 126 & 255 ((reserved values)");
                    choose_nodeid.Clear();
                }
                else
                {
                    //set the node id!
                    String command = null;
                    //creating the XML command
                    command = String.Concat(command, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
                    command = String.Concat(command, "<methodName>xmesh.set_nodeid</methodName>");
                    command = String.Concat(command, "<params><param><value><struct>");
                    command = String.Concat(command, "<member><name>destAddress</name><value><int>" + this.node_id + "</int></value></member>");
                    command = String.Concat(command, "<member><name>groupId</name><value><int>" + this.group_id + "</int></value></member>");
                    command = String.Concat(command, "<member><name>newNodeId</name><value><int>" + Int32.Parse(choose_nodeid.Text) + "</int></value></member>");
                    command = String.Concat(command, "</struct></value></param></params></methodCall>");
                    //sending command, receiving response
                    try
                    {
                        byte[] response = SendCommand(command);
                        if (response == null)
                        throw new MotePacketException("Error: No response received\n");
                        //need to translate result and retrieve node info
                        response_stream = new MemoryStream(response);
                        xmlreader = new XmlTextReader(response_stream);
                        node_config.Clear();
                        string temp;
                        while (!xmlreader.EOF)
                        {
                            if(xmlreader.ReadToFollowing("name"))
                            {
                                if (xmlreader.Read())
                                {
                                    temp = xmlreader.ReadContentAsString();
                                    switch (temp)
                                    {
                                        case "codeName":
                                            if (xmlreader.ReadToFollowing("string"))
                                            {
                                                if (xmlreader.Read())
                                                    response_log.AppendText(xmlreader.ReadContentAsString() + ": ");
                                                else
                                                    throw new MotePacketException("Dirty response packet.\n");
                                            }
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                            break;
                                        case "codeDescription":
                                            if (xmlreader.ReadToFollowing("string"))
                                            {
                                                if (xmlreader.Read())
                                                    response_log.AppendText(xmlreader.ReadContentAsString() + "\n");
                                                else
                                                    throw new MotePacketException("Dirty response packet.\n");
                                            }
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                            break;
                                        case "nodeId":
                                            if (xmlreader.ReadToFollowing("int"))
                                            {
                                                if (xmlreader.Read())
                                                    response_log.AppendText("New node ID : " + xmlreader.ReadContentAsInt() + "\n");
                                                else
                                                    throw new MotePacketException("Dirty response packet.\n");
                                            }
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                            break;
                                    }
                                }
                                    else throw new MotePacketException("Dirty response packet.\n");                      
                             }   
                         }   
                    }
                    catch (Exception ex)
                    {
                        response_log.AppendText(ex.Message);
                    }
                    finally
                    {
                        if (xmlreader != null)
                            xmlreader.Close();
                        if (response_stream != null)
                            response_stream.Close();
                    }
                }
            }

        }

        private void button_setgroupid_Click(object sender, EventArgs e)
        {
            if (Int32.Parse(choose_groupid.Text) >= 0 && Int32.Parse(choose_groupid.Text) < 255)
            {
                if (Int32.Parse(choose_groupid.Text) == 126)
                {
                    MessageBox.Show("Invalid group ID.\nRange: 0-254, excluding 126 (reserved value)");
                    choose_groupid.Clear();
                }
                else
                {
                    //set the group id!
                    String command = null;
                    //creating the XML command
                    command = String.Concat(command, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
                    command = String.Concat(command, "<methodName>xmesh.setgroupid</methodName>");
                    command = String.Concat(command, "<params><param><value><struct>");
                    command = String.Concat(command, "<member><name>destAddress</name><value><int>" + this.node_id + "</int></value></member>");
                    command = String.Concat(command, "<member><name>groupId</name><value><int>" + this.group_id + "</int></value></member>");
                    command = String.Concat(command, "<member><name>newGroupId</name><value><int>" + Int32.Parse(choose_groupid.Text) + "</int></value></member>");
                    command = String.Concat(command, "</struct></value></param></params></methodCall>");
                    //sending command, receiving response
                    try
                    {
                        byte[] response = SendCommand(command);
                                        if (response == null)
                        throw new MotePacketException("Error: No response received\n");
                        //need to translate result and retrieve node info
                        response_stream = new MemoryStream(response);
                        xmlreader = new XmlTextReader(response_stream);
                        node_config.Clear();
                        string temp;
                        while (!xmlreader.EOF)
                        {
                            if(xmlreader.ReadToFollowing("name"))
                            {
                                if (xmlreader.Read())
                                {
                                    temp = xmlreader.ReadContentAsString();
                                    switch (temp)
                                    {
                                        case "codeName":
                                            if (xmlreader.ReadToFollowing("string"))
                                            {
                                                if (xmlreader.Read())
                                                    response_log.AppendText(xmlreader.ReadContentAsString() + ": ");
                                                else
                                                    throw new MotePacketException("Dirty response packet.\n");
                                            }
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                            break;
                                        case "codeDescription":
                                            if (xmlreader.ReadToFollowing("string"))
                                            {
                                                if (xmlreader.Read())
                                                    response_log.AppendText(xmlreader.ReadContentAsString() + "\n");
                                                else
                                                    throw new MotePacketException("Dirty response packet.\n");
                                            }
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                            break;

                                        case "nodeId":
                                            if (xmlreader.ReadToFollowing("int"))
                                            {
                                                if (xmlreader.Read())
                                                    response_log.AppendText("Node : " +xmlreader.ReadContentAsInt() + "\t");
                                                else
                                                    throw new MotePacketException("Dirty response packet.\n");
                                            }
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                            break;

                                        case "groupId":
                                            if (xmlreader.ReadToFollowing("int"))
                                            {
                                                if (xmlreader.Read())
                                                    response_log.AppendText("New group ID : " +xmlreader.ReadContentAsInt() + "\n");
                                                else
                                                    throw new MotePacketException("Dirty response packet.\n");
                                            }
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                            break;
                                    }
                                }
                                    else throw new MotePacketException("Dirty response packet.\n");                      
                             }   
                         }  
                    }
                    catch (Exception ex)
                    {
                        response_log.AppendText(ex.Message);
                    }
                    finally
                    {
                        if (xmlreader != null)
                            xmlreader.Close();
                        if (response_stream != null)
                            response_stream.Close();
                    }
                }
            }
        }

        private void button_setrfchannel_Click(object sender, EventArgs e)
        {
            if (Int32.Parse(choose_rfchannel.Text) >= 11 && Int32.Parse(choose_rfchannel.Text) < 27)
            {
                    //set rf channel id!
                String command = null;
                //creating the XML command
                command = String.Concat(command, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
                command = String.Concat(command, "<methodName>xmesh.set_rfchannel</methodName>");
                command = String.Concat(command, "<params><param><value><struct>");
                command = String.Concat(command, "<member><name>destAddress</name><value><int>" + this.node_id + "</int></value></member>");
                command = String.Concat(command, "<member><name>groupId</name><value><int>" + this.group_id + "</int></value></member>");
                command = String.Concat(command, "<member><name>newRfChannel</name><value><int>" + Int32.Parse(choose_rfchannel.Text) + "</int></value></member>");
                command = String.Concat(command, "</struct></value></param></params></methodCall>");
                //sending command, receiving response
                try
                {
                    byte[] response = SendCommand(command);
                                    if (response == null)
                    throw new MotePacketException("Error: No response received\n");
                    //need to translate result and retrieve node info
                    response_stream = new MemoryStream(response);
                    xmlreader = new XmlTextReader(response_stream);
                    node_config.Clear();
                    string temp;
                    while (!xmlreader.EOF)
                    {
                        if(xmlreader.ReadToFollowing("name"))
                        {
                            if (xmlreader.Read())
                            {
                                temp = xmlreader.ReadContentAsString();
                                switch (temp)
                                {
                                    case "codeName":
                                        if (xmlreader.ReadToFollowing("string"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText(xmlreader.ReadContentAsString() + ": ");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;
                                    case "codeDescription":
                                        if (xmlreader.ReadToFollowing("string"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText(xmlreader.ReadContentAsString() + "\n");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;
                                    case "nodeId":
                                        if (xmlreader.ReadToFollowing("int"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText("Node : " +xmlreader.ReadContentAsInt() + "\t");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;

                                    case "rfChannel":
                                        if (xmlreader.ReadToFollowing("int"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText("New RF Channel : " +xmlreader.ReadContentAsInt() + "\n");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;
                                }
                            }
                                else throw new MotePacketException("Dirty response packet.\n");                      
                         }   
                     }  
                }
                catch (Exception ex)
                {
                    response_log.AppendText(ex.Message);
                }
                finally
                {
                    if (xmlreader != null)
                        xmlreader.Close();
                    if (response_stream != null)
                        response_stream.Close();
                }
            }
        }

        private void button_setrfpower_Click(object sender, EventArgs e)
        {
            if (Int32.Parse(choose_rfpower.Text) >= 0 && Int32.Parse(choose_rfpower.Text) < 16)
            {
                //set the group id!
                String command = null;
                //creating the XML command
                command = String.Concat(command, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
                command = String.Concat(command, "<methodName>xmesh.set_rate</methodName>");
                command = String.Concat(command, "<params><param><value><struct>");
                command = String.Concat(command, "<member><name>destAddress</name><value><int>" + this.node_id + "</int></value></member>");
                command = String.Concat(command, "<member><name>groupId</name><value><int>" + this.group_id + "</int></value></member>");
                command = String.Concat(command, "<member><name>newRate</name><value><int>" + Int32.Parse(choose_samplerate.Text) + "</int></value></member>");
                command = String.Concat(command, "</struct></value></param></params></methodCall>");
                //sending command, receiving response
                try
                {
                    byte[] response = SendCommand(command);
                                    if (response == null)
                    throw new MotePacketException("Error: No response received\n");
                    //need to translate result and retrieve node info
                    response_stream = new MemoryStream(response);
                    xmlreader = new XmlTextReader(response_stream);
                    node_config.Clear();
                    string temp;
                    while (!xmlreader.EOF)
                    {
                        if(xmlreader.ReadToFollowing("name"))
                        {
                            if (xmlreader.Read())
                            {
                                temp = xmlreader.ReadContentAsString();
                                switch (temp)
                                {
                                    case "codeName":
                                        if (xmlreader.ReadToFollowing("string"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText(xmlreader.ReadContentAsString() + ": ");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;
                                    case "codeDescription":
                                        if (xmlreader.ReadToFollowing("string"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText(xmlreader.ReadContentAsString() + "\n");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;
                                    case "nodeId":
                                        if (xmlreader.ReadToFollowing("int"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText("Node : " +xmlreader.ReadContentAsInt() + "\t");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;

                                    case "rfPower":
                                        if (xmlreader.ReadToFollowing("int"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText("New RF Power : " +xmlreader.ReadContentAsInt() + "\n");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;
                                }
                            }
                                else throw new MotePacketException("Dirty response packet.\n");                      
                         }   
                     }
                }
                catch (Exception ex)
                {
                    response_log.AppendText(ex.Message);
                }
                finally
                {
                    if (xmlreader != null)
                        xmlreader.Close();
                    if (response_stream != null)
                        response_stream.Close();
                }
            }
        }

        private void button_setsamplerate_Click(object sender, EventArgs e)
        {
            if (Int32.Parse(choose_samplerate.Text) >= 500 && Int32.Parse(choose_samplerate.Text) <= 100000)
            {
                //set the group id!
                String command = null;
                //creating the XML command
                command = String.Concat(command, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
                command = String.Concat(command, "<methodName>xmesh.set_nodeid</methodName>");
                command = String.Concat(command, "<params><param><value><struct>");
                command = String.Concat(command, "<member><name>destAddress</name><value><int>" + this.node_id + "</int></value></member>");
                command = String.Concat(command, "<member><name>groupId</name><value><int>" + this.group_id + "</int></value></member>");
                command = String.Concat(command, "<member><name>newRfChannel</name><value><int>" + Int32.Parse(choose_rfchannel.Text) + "</int></value></member>");
                command = String.Concat(command, "</struct></value></param></params></methodCall>");
                //sending command, receiving response
                try
                {
                    byte[] response = SendCommand(command);
                    if (response == null)
                        throw new MotePacketException("Error: No response received\n");
                        //need to translate result and retrieve node info
                        response_stream = new MemoryStream(response);
                    xmlreader = new XmlTextReader(response_stream);
                    node_config.Clear();
                    string temp;
                    while (!xmlreader.EOF)
                    {
                        if(xmlreader.ReadToFollowing("name"))
                        {
                            if (xmlreader.Read())
                            {
                                temp = xmlreader.ReadContentAsString();
                                switch (temp)
                                {
                                    case "codeName":
                                        if (xmlreader.ReadToFollowing("string"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText(xmlreader.ReadContentAsString() + ": ");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;
                                    case "codeDescription":
                                        if (xmlreader.ReadToFollowing("string"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText(xmlreader.ReadContentAsString() + "\n");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;

                                    case "nodeId":
                                        if (xmlreader.ReadToFollowing("int"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText("Node : " + xmlreader.ReadContentAsInt() + "\t");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;

                                    case "rate":
                                        if (xmlreader.ReadToFollowing("int"))
                                        {
                                            if (xmlreader.Read())
                                                response_log.AppendText("New rate: " + xmlreader.ReadContentAsInt() + " ms\n");
                                            else
                                                throw new MotePacketException("Dirty response packet.\n");
                                        }
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                        break;
  
                                }
                            }
                                else throw new MotePacketException("Dirty response packet.\n");                      
                         }   
                     }
                }
                catch (Exception ex)
                {
                    response_log.AppendText(ex.Message);
                }
                finally
                {
                    if (xmlreader != null)
                        xmlreader.Close();
                    if (response_stream != null)
                        response_stream.Close();
                }
            }
        }

        private void button_reset_Click(object sender, EventArgs e)
        {
            String command = null;
            //creating the XML command
            command = String.Concat(command, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
            command = String.Concat(command, "<methodName>xmesh.reset</methodName>");
            command = String.Concat(command, "<params><param><value><struct>");
            command = String.Concat(command, "<member><name>destAddress</name><value><int>" + this.node_id + "</int></value></member>");
            command = String.Concat(command, "<member><name>groupId</name><value><int>" + this.group_id + "</int></value></member>");
            command = String.Concat(command, "</struct></value></param></params></methodCall>");
            //sending command, receiving response
            try
            {
                byte[] response = SendCommand(command);
                if (response == null)
                    throw new MotePacketException("Error: No response received\n");
                //need to translate result and retrieve node info
                response_stream = new MemoryStream(response);
                xmlreader = new XmlTextReader(response_stream);
                node_config.Clear();
                string temp;
                while (!xmlreader.EOF)
                {
                    if (xmlreader.ReadToFollowing("name"))
                    {
                        if (xmlreader.Read())
                        {
                            temp = xmlreader.ReadContentAsString();
                            switch (temp)
                            {
                                case "codeName":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            response_log.AppendText(xmlreader.ReadContentAsString() + ": ");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;
                                case "codeDescription":
                                    if (xmlreader.ReadToFollowing("string"))
                                    {
                                        if (xmlreader.Read())
                                            response_log.AppendText(xmlreader.ReadContentAsString() + "\n");
                                        else
                                            throw new MotePacketException("Dirty response packet.\n");
                                    }
                                    else
                                        throw new MotePacketException("Dirty response packet.\n");
                                    break;

                            }
                        }
                        else throw new MotePacketException("Dirty response packet.\n");
                    }
                }
            }
            catch (Exception ex)
            {
                response_log.AppendText(ex.Message);
            }
            finally
            {
                if (xmlreader != null)
                    xmlreader.Close();
                if (response_stream != null)
                    response_stream.Close();
            }
        }

    }
}
